﻿/**
 * 年月选择插件
 * chenjun 20130217
 */
(function($) {
	$.fn.ymcalendar = function(options, param) {
		if (typeof options == 'string'){
			return $.fn.ymcalendar.methods[options](this, param);
		}
		options = options || {};
		return this.each(function() {
			var target = $(this);
			var state = $.data(this, 'ymcalendar');
			if (state){
				$.extend(state.options, options);
			}
			else {
				$.data(this, 'ymcalendar', {
					options: $.extend({}, $.fn.ymcalendar.defaults, {
						disabled: (target.attr('disabled') ? true : false),
						onchange: target[0].onchange
					}, options)
				});
				target[0].onchange = null;
				target.removeAttr('disabled');
			}
			create(target);
		});
	};

	$.fn.ymcalendar.defaults = {
		disabled: false
	};

	$.fn.ymcalendar.methods = {
		enable: function(jq) {
			return jq.each(function() {
				setDisabled(this, false);
			});
		},
		disable: function(jq){
			return jq.each(function(){
				setDisabled(this, true);
			});
		}
	};

	function create(target) {
		var state = $.data(target[0], 'ymcalendar');
		target.keydown(function() {
			return false;
		});
		target.click(function() {
			var year;
			var flag = true;
			var ymcalendar = $("<div class='ymcalendar-Div' style='left:"+target.offset().left+"px;top:"+(target.offset().top+target.height()+4)+"px'></div>");
			var tab = $("<table class='tabDate' border='0' cellpadding='0' cellspacing='1px'></table>");
			var tr = $("<tr></tr>");
			var td = $("<td class='ymcalendar-text'>&lt;&lt;</td>");
			td.mousedown(function() {
				flag = false;
				year.attr("year", parseInt(year.attr("year")) - 1);
				year.html(year.attr("year") + "年");
                oldYear=$(".ymcalendar").val();
                oldMon=oldYear.substring(4);
                var newChar=oldYear.substr(0,4);
                newChar=newChar*1-1;
                newChar=newChar+oldMon;
                $(".ymcalendar").val(newChar);
                
               
			});
			td.mouseup(function() {
				flag = true;
				target[0].focus();
                
			});
			tr.append(td);

			var y = new Date().getFullYear();
            
			y = target.val() != "" ? target.val().substring(0,target.val().length-4) : y;
			year = $("<td class='ymcalendar-text year' year='"+y+"' colspan='2'>"+y+"年</td>");
			tr.append(year);

			td = $("<td class='ymcalendar-text'>&gt;&gt;</td>");
			td.mousedown(function() {
				flag = false;
				year.attr("year", parseInt(year.attr("year")) + 1);
				year.html(year.attr("year") + "年");
                
                oldYear=$(".ymcalendar").val();
                oldMon=oldYear.substring(4);
                var newChar=oldYear.substr(0,4);
                newChar=newChar*1+1;
                newChar=newChar+oldMon;
                $(".ymcalendar").val(newChar);
			});
			td.mouseup(function() {
				flag = true;
				target[0].focus();
			});
			tr.append(td);
			tab.append(tr);

			var m = target.val() != "" ? Number(target.val().substring(target.val().length-2)) : 0;
			for (var i = 0; i < 12; i++) {
				if (i%4 == 0) {
					tr = $("<tr></tr>");
					tab.append(tr);
				}
				var cls;
				if (m == i+1) cls = "ymcalendar-selected";
				else cls = "ymcalendar-text";
				td = $("<td class='"+cls+"'>"+(i+1)+"月</td>");
				td.mousedown(function() {
                    $(".loadingImg").css("display","block"); 
					var mon = $(this).html().replace("月","");
					mon = mon.length==1?"0"+mon:mon;
                    var newMon=mon;
                    mon="年"+mon+"月";
					var newval = year.attr("year")+mon;
                   $(".ymcalendar").attr("data",newval);
					if (target.val() != newval) {
						target.val(year.attr("year")+mon);

						if (state.options.onchange)
							state.options.onchange();
					}
                    
                function fix(num, length) {
                    return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
                }
                    var txt=$(".ymcalendar").val();
                    txt=txt.substr(5,2);
                    $(".preMonth").text(fix(txt*1-1,2));
                    $(".nextMonth").text(fix(txt*1+1,2));
                    var list      = $(".countType .active").val();
                    charChange(list)
                    if(newMon=="12"){
                      $(".nextMonth").text("01");     
                    }else if(newMon=="01"){
                        $(".preMonth").text("12");  
                    }
                   
				});
				tr.append(td);
			}
			ymcalendar.append(tab);

			tab.children().find("td").mouseover(function() {
				var cls = $(this).attr("class");
				$(this).removeAttr("class");
				if (cls.indexOf("selected")>0)
					$(this).addClass("ymcalendar-selected-over");
				else
					$(this).addClass("ymcalendar-text-over");
			});
			tab.children().find("td").mouseout(function() {
				var cls = $(this).attr("class");
				$(this).removeAttr("class");
				if (cls.indexOf("selected")>0)
					$(this).addClass("ymcalendar-selected");
				else
					$(this).addClass("ymcalendar-text");
			});
			year.unbind("mouseover");
			year.unbind("mouseout");

			$(document.body).append(ymcalendar);
			target.attr("box", ymcalendar);
			target.blur(function(){
				if (flag) {
					ymcalendar.remove();
					target.attr("box", null);
				}
			});
		});

		if (state.options.disabled && !target.attr('disabled')) {
			setDisabled(target[0], true);
		}
		else if (!state.options.disabled && target.attr('disabled')) {
			setDisabled(target[0], false);
		}
	}

	function setDisabled(target, disabled) {
		var state = $.data(target, 'ymcalendar');
		if (disabled) {
			state.options.disabled = true;
			if (target.onclick){
				state.onclick = target.onclick;
				target.onclick = null;
			}
			$(target).attr('disabled',true);
		}
		else {
			state.options.disabled = false;
			if (state.onclick) {
				target.onclick = state.onclick;
			}
			$(target).attr('disabled',false);
		}
		return false;
	}
})(jQuery);

$(function() { $(".ymcalendar").ymcalendar(); });

$(document).ready(function(){
    function fix(num, length) {
        return ('' + num).length < length ? ((new Array(length + 1)).join('0') + num).slice(-length) : '' + num;
    }
    var date=new Date;
    var year=date.getFullYear(); 
    var month=date.getMonth()+1;
    month=fix(month,2);
    var ymNew=year+"年"+month+"月";
    $(".ymcalendar").attr("value",ymNew);
    $(".ymcalendar").val(ymNew);
    $(".ymcalendar").live("click",function(){ 
        var m=$(this).val();
        n=m.substr(0,4);
        m=m.substr(5,2);
        var i=m%4;
        var j=parseInt(m/4);
        $(".tabDate tr").eq(j+1).find("td").eq(i-1).addClass("ymcalendar-selected"); 
        $(".tabDate tr").eq(j+1).find("td").eq(i-1).css("background","#0797D9");
        $(".tabDate tr").eq(0).find("td").css("background","#E0ECFF"); 
    });
    $(".preMonth").text(fix(month*1-1,2));
    $(".nextMonth").text(fix(month*1+1,2));
    $(".preMonthBox").click(function(){
        var mm=$(".ymcalendar").val();
        var year=mm.substr(0,4);
        var mon=mm.substr(5,2);
        mon*=1;
            if(mon>2){
               mon-=1;
                var yM=year+"年"+fix(mon,2)+"月";
                var nextM=fix(mon+1,2)
                var preM=fix(mon-1,2)
                $(".preMonth").text(preM);
                $(".nextMonth").text(nextM);
                $(".ymcalendar").val(yM);
                
            }else if(mon==2){
                yM=year+"年"+"01月";
                $(".preMonth").text("12");
                $(".ymcalendar").val(yM);
                $(".nextMonth").text("02");
            }else{
                year-=1;
               yM=year+"年"+"12月";
                $(".preMonth").text("11");
                $(".ymcalendar").val(yM);
                $(".nextMonth").text("01");
            }
    })
     
    $(".nextMonthBox").click(function(){
        var mm=$(".ymcalendar").val();
        var year=mm.substr(0,4);
        var mon=mm.substr(5,2);
        year*=1;
        mon*=1;
            if(mon>=1&&mon<11){
               mon+=1;
                var yM=year+"年"+fix(mon,2)+"月";
                var nextM=fix(mon+1,2)
                var preM=fix(mon-1,2)
                $(".preMonth").text(preM);
                $(".nextMonth").text(nextM);
                $(".ymcalendar").val(yM);
                
            }else if(mon==11){
                yM=year+"年"+"12月";
                $(".preMonth").text("11");
                $(".ymcalendar").val(yM);
                $(".nextMonth").text("01");
            }else if(mon==12){
                year+=1;
               yM=year+"年"+"01月";
                $(".preMonth").text("12");
                $(".ymcalendar").val(yM);
                $(".nextMonth").text("02");
            }
    })
    var time      = $("#ymcalendar").val();
    var list      = $(".countType .active").val();
    var xc='',tc='',lmm='',tn='',qne='',mt='',zgzx='',fx='',tb='',qt=''
    $.ajax({
        url : "http://www.12301.cc/module/wxh/monthly/monthly.html",
        type : "GET",
        dataType : "json",
        data : {
            action    : 5,
            list      : list,
            time      : time
        },
        success:function(res){
            for(var  i in res) {
                if(i=="zgzx"){
                    if(list==2){
                        zgzx=res[i]['s']/100
                    }else{
                        zgzx=res[i]['s']
                    }
                }else if(i=="fx"){
                    if(list==2){
                        fx=res[i]['s']/100
                    }else{
                        fx=res[i]['s']
                    }
                }else  if(i==1214){
                    if(list==2){
                        xc=res[i]['s']/100
                    }else{
                        xc=res[i]['s']
                    }
                }else if(i==1454){
                    if(list==2){
                        tc=res[i]['s']/100
                    }else{
                        tc=res[i]['s']
                    }
                }else if(i==1472){
                    if(list==2){
                        lmm=res[i]['s']/100
                    }else{
                        lmm=res[i]['s']
                    }
                }else if(i==3034){
                    if(list==2){
                        tn=res[i]['s']/100
                    }else{
                        tn=res[i]['s']
                    }
                }else if(i==2175){
                    if(list==2){
                        qne=res[i]['s']/100
                    }else{
                        qne=res[i]['s']
                    }
                }else if(i==1853){
                    if(list==2){
                        mt=res[i]['s']/100
                    } else{
                        mt=res[i]['s']
                    }
                }else if(i==3035){
                    if(list==2){
                        tb=res[i]['s']/100
                    }else{
                        tb=res[i]['s']
                    }
                }else if(i=="qt"){
                    if(list==2){
                        qt=res[i]['s']/100
                    }else{
                        qt=res[i]['s']
                    }
                }
                $(".setAbsolute .setFont_t").html(res['tnums'])
                $(".setAbsolute .setFont_m").html(res['moneys']+'元')
            }
            
            option = {
                title : {
                    text: '分销渠道',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient : 'vertical',
                    x : 'left',
                    data:['自供自销','OTA分销','携程网','同程网','驴妈妈','南京途牛旅游网','去哪儿','美团','淘宝码商','其他']

                },
                color:['#2EC7C9','#5AB1EF','#2EC7C9','#95706C','#D97A81','#BA4B12','#8D99B3','#FFB981','#97B552','#E5CF0C'],
                toolbox: {
                    show : true,
                    feature : {
                        dataView : {show: true, readOnly: false},
                        magicType : {
                            show: true,
                            type: ['pie', 'funnel'],
                            option: {
                                funnel: {
                                    x: '25%',
                                    width: '50%',
                                    funnelAlign: 'left',
                                    max: 1548
                                }
                            }
                        },
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                calculable : false,
                series : [
                    {
                        name:'分销渠道',
                        type:'pie',
                        selectedMode: 'single',
                        radius : [0, 70],

                        x: '20%',
                        width: '40%',
                        funnelAlign: 'right',
                        max: 1548,
                        itemStyle : {
                            normal : {
                                label : {
                                    position : 'inner'
                                },
                                labelLine : {
                                    show : false
                                }
                            }
                        },
                        data:[
                            {value:zgzx, name:'自供自销'},
                            {value:fx, name:'OTA分销'},

                        ]
                    },
                    {
                        name:'分销渠道',
                        type:'pie',
                        radius : [100, 140],
                        x: '60%',
                        width: '35%',
                        funnelAlign: 'left',
                        max: 1048,

                        data:[
                            {value:xc, name:'携程网'},
                            {value:tc, name:'同程网'},
                            {value:lmm, name:'驴妈妈'},
                            {value:tn, name:'南京途牛旅游网'},
                            {value:qne, name:'去哪儿'},
                            {value:mt, name:'美团'},
                            {value:tb, name:'淘宝码商'},
                            {value:qt, name:'其他'},
                        ]
                    }
                ]
            };

            var myChart = echarts.init(document.getElementById('chart'));
            myChart.setOption(option);
            $(".loadingImg").css("display","none");
        }
    })
    $(".preMonthBox").on('click', function() {
        var list      = $(".countType .active").val();
        charChange(list)
    })
    $(".nextMonthBox").on('click', function() {
        var list      = $(".countType .active").val();
        charChange(list)
    })

    $(".countType .list").live('click',function(e) {
        $(this).siblings().removeClass("active");
        $(this).addClass("active");
        var list      = $(this).val();
        charChange(list)
    })

    charChange = function(list){
        var time      = $("#ymcalendar").val();
        var xc='',tc='',lmm='',tn='',qne='',mt='',zgzx='',fx='',tb='',qt=''
        $.ajax({
            url : "http://www.12301.cc/module/wxh/monthly/monthly.html",
            type : "GET",
            dataType : "json",
            data : {
                action    : 5,
                list      : list,
                time      : time
            },
            success:function(res){
                if(res.status=='failure') alert(res.msg)
                for(var  i in res) {
                    if(i=="zgzx"){
                        if(list==2){
                            alert(res[i]['s'])
                            zgzx=res[i]['s']/100
                        }else{
                            zgzx=res[i]['s']
                        }
                    }else if(i=="fx"){
                        if(list==2){
                            fx=res[i]['s']/100
                        }else{
                            fx=res[i]['s']
                        }
                    }else  if(i==1214){
                        if(list==2){
                            xc=res[i]['s']/100
                        }else{
                            xc=res[i]['s']
                        }
                    }else if(i==1454){
                        if(list==2){
                            tc=res[i]['s']/100
                        }else{
                            tc=res[i]['s']
                        }
                    }else if(i==1472){
                        if(list==2){
                            lmm=res[i]['s']/100
                        }else{
                            lmm=res[i]['s']
                        }
                    }else if(i==3034){
                        if(list==2){
                            tn=res[i]['s']/100
                        }else{
                            tn=res[i]['s']
                        }
                    }else if(i==2175){
                        if(list==2){
                            qne=res[i]['s']/100
                        }else{
                            qne=res[i]['s']
                        }
                    }else if(i==1853){
                        if(list==2){
                            mt=res[i]['s']/100
                        } else{
                            mt=res[i]['s']
                        }
                    }else if(i==3035){
                        if(list==2){
                            tb=res[i]['s']/100
                        }else{
                            tb=res[i]['s']
                        }
                    }else if(i=="qt"){
                        if(list==2){
                            qt=res[i]['s']/100
                        }else{
                            qt=res[i]['s']
                        }
                    }
                    $(".setAbsolute .setFont_t").html(res['tnums']+0)
                    $(".setAbsolute .setFont_m").html(res['moneys']+0+'元')
                }

                option = {
                    title : {
                        text: '分销渠道',
                        x:'center'
                    },
                    tooltip : {
                        trigger: 'item',
                        formatter: "{a} <br/>{b} : {c} ({d}%)"
                    },
                    color:['#2EC7C9','#5AB1EF','#2EC7C9','#95706C','#D97A81','#BA4B12','#8D99B3','#FFB981','#97B552','#E5CF0C'],
                    legend: {
                        orient : 'vertical',
                        x : 'left',
                        data:['自供自销','OTA分销','携程网','同程网','驴妈妈','南京途牛旅游网','去哪儿','美团','淘宝码商','其他']
                    },
                    toolbox: {
                        show : true,
                        feature : {
                            dataView : {show: true, readOnly: false},
                            magicType : {
                                show: true,
                                type: ['pie', 'funnel'],
                                option: {
                                    funnel: {
                                        x: '25%',
                                        width: '50%',
                                        funnelAlign: 'left',
                                        max: 1548
                                    }
                                }
                            },
                            restore : {show: true},
                            saveAsImage : {show: true}
                        }
                    },
                    calculable : false,
                    series : [
                        {
                            name:'分销渠道',
                            type:'pie',
                            selectedMode: 'single',
                            radius : [0, 70],

                            x: '20%',
                            width: '40%',
                            funnelAlign: 'right',
                            max: 1548,
                            itemStyle : {
                                normal : {
                                    label : {
                                        position : 'inner'
                                    },
                                    labelLine : {
                                        show : false
                                    }
                                }
                            },
                            data:[
                                {value:zgzx, name:'自供自销'},
                                {value:fx, name:'OTA分销'},

                            ]
                        },
                        {
                            name:'分销渠道',
                            type:'pie',
                            radius : [100, 140],
                            x: '60%',
                            width: '35%',
                            funnelAlign: 'left',
                            max: 1048,

                            data:[
                                {value:xc, name:'携程网'},
                                {value:tc, name:'同程网'},
                                {value:lmm, name:'驴妈妈'},
                                {value:tn, name:'南京途牛旅游网'},
                                {value:qne, name:'去哪儿'},
                                {value:mt, name:'美团'},
                                {value:tb, name:'淘宝码商'},
                                {value:qt, name:'其他'},
                            ]
                        }
                    ]
                };

                var myChart = echarts.init(document.getElementById('chart'));
                myChart.setOption(option);
                $(".loadingImg").css("display","none");
            }
        })

    }


})
